﻿using Kaskelot.SP.UI.Analys.Data.Mapping;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace Kaskelot.SP.UI.Analys.Data
{
	public class AnalyseServiceWrapper
	{
		public string ServiceUrl { get; set; }

		private static AnalyseServiceWrapper instance;

		public static AnalyseServiceWrapper Instance
		{
			get
			{
				if (instance == null)
					instance = new AnalyseServiceWrapper();
				return instance;
			}
		}

		public void GetEntities(Action<ObservableCollection<ViewModel.Entity>> onSuccess, Action<Exception> onError)
		{
			if (ServiceUrl == null)
				return;

			var factory = WCFHelper.GetServiceFactory(new Uri(ServiceUrl));
			factory.GetEntitiesCompleted += (source, result) =>
			{
				if (result.Error != null)
				{
					onError(result.Error);
				}
				else
				{
					var collection = new ObservableCollection<ViewModel.Entity>();
					foreach (var item in result.Result)
					{
						collection.Add(item.ToEntityViewModel(false));
					}
					onSuccess(collection);
				}

				factory.CloseAsync();
			};
			factory.GetEntitiesAsync();
		}

		public void ExecuteQuery(ViewModel.Query query, Action<ViewModel.QueryResult> onSuccess, Action<Exception> onError)
		{
			if (ServiceUrl == null)
				return;

			var factory = WCFHelper.GetServiceFactory(new Uri(ServiceUrl));
			factory.ExecuteQueryCompleted += (source, result) =>
			{
				if (result.Error != null)
				{
					onError(result.Error);
				}
				else if (result.Result != null && result.Result.HasError)
				{
					onError(new Exception(result.Result.ErrorMessage));
				}
				else
				{
					onSuccess(result.Result.ToQueryResultViewModel());
				}

				factory.CloseAsync();
			};

			factory.ExecuteQueryAsync(query.ToQueryServiceModel());
		}

		public void GetLookupInformation(Action<List<ViewModel.LookupField>> onSuccess, Action<Exception> onError)
		{
			if (ServiceUrl == null)
				return;

			var factory = WCFHelper.GetServiceFactory(new Uri(ServiceUrl));
			factory.GetLookupInformationCompleted += (source, result) =>
			{
				if (result.Error != null)
					onError(result.Error);
				else
					onSuccess(result.Result.Select(d => d.ToLookupFieldViewModel()).ToList());


				factory.CloseAsync();
			};
			factory.GetLookupInformationAsync();
		}

		public void GetPersonalQueries(Action<List<ViewModel.Query>> onSuccess, Action<Exception> onError)
		{
			if (ServiceUrl == null)
				return;

			var factory = WCFHelper.GetServiceFactory(new Uri(ServiceUrl));
			factory.GetPersonalQueriesCompleted += (source, result) =>
			{
				if (result.Error != null)
					onError(result.Error);
				else
					onSuccess(result.Result.Select(d => d.ToQueryViewModel()).ToList());

				factory.CloseAsync();
			};
			factory.GetPersonalQueriesAsync();
		}

		public void SavePersonalQuery(ViewModel.Query query, Action<string> onSuccess, Action<Exception> onError)
		{
			if (ServiceUrl == null)
				return;

			var factory = WCFHelper.GetServiceFactory(new Uri(ServiceUrl));
			factory.SavePersonalQueryCompleted += (source, result) =>
			{
				if (result.Error != null)
					onError(result.Error);
				else
					onSuccess("Urval sparat!");

				factory.CloseAsync();
			};
			factory.SavePersonalQueryAsync(query.ToQueryServiceModel());
		}

		public void SaveGlobalQuery(ViewModel.Query query, Action<string> onSuccess, Action<Exception> onError)
		{
			if (ServiceUrl == null)
				return;

			var factory = WCFHelper.GetServiceFactory(new Uri(ServiceUrl));
			factory.SaveGlobalQueryCompleted += (source, result) =>
			{
				if (result.Error != null)
					onError(result.Error);
				else
					onSuccess("Urval sparat!");

				factory.CloseAsync();
			};
			factory.SaveGlobalQueryAsync(query.ToQueryServiceModel());
		}

		public void DeleteQuery(int id, Action<string> onSuccess, Action<Exception> onError)
		{
			if (ServiceUrl == null)
				return;

			var factory = WCFHelper.GetServiceFactory(new Uri(ServiceUrl));
			factory.DeleteQueryCompleted += (source, result) =>
			{
				if (result.Error != null)
					onError(result.Error);
				else
					onSuccess("Urval borttaget!");

				factory.CloseAsync();
			};
			factory.DeleteQueryAsync(id);
		}

		public void GetAnalyseInfoLinkUrl(Action<string> onSucess, Action<Exception> onError)
		{
			if (ServiceUrl == null)
				return;

			//var factory = WCFHelper.GetServiceFactory(new Uri(ServiceUrl));
			//factory.GetAnalyseInfoLinkUrlCompleted += (source, result) =>
			//    {
			//        if (result.Error != null)
			//            onError(result.Error);
			//        else
			//            onSucess(result.Result);

			//        factory.CloseAsync();
			//    };
			//factory.GetAnalyseInfoLinkUrlAsync();
		}
	}
}
